﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace SharpOSCAR.Base
{
    /// <summary>
    /// A simple array of TLVs without any block header
    /// </summary>
    public class TlvArray : ProtocolData, ITlvContainer
    {
        private LinkedList<Tlv> _TLVs;

        /// <summary>
        /// Initializes the TlvArray
        /// </summary>
        public TlvArray()
        {
            _TLVs = new LinkedList<Tlv>();
        }

        /// <summary>
        /// Adds a new Tlv to the TlvArray
        /// </summary>
        /// <param name="value">Tlv to add</param>
        public void AddTlv(Tlv value)
        {
            _TLVs.AddLast(value);
        }

        /// <summary>
        /// Removes a Tlv from the TlvArray
        /// </summary>
        /// <param name="value">Tlv to remove</param>
        public void RemoveTlv(Tlv value)
        {
            _TLVs.Remove(value);
        }

        /// <summary>
        /// Gets the number of Tlvs in the TlvArray
        /// </summary>
        public int Count
        {
            get { return _TLVs.Count; }
        }

        /// <summary>
        /// Converts the TlvArray into a byte array
        /// </summary>
        /// <returns>Returns an array of bytes representing the TlvArray</returns>
        public override byte[] ToByteArray()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                foreach (Tlv value in _TLVs)
                    value.WriteToStream(ms);
                return ms.ToArray();
            }
        }
    }
}
